CC = gcc
CFLAGS = -O0 -Wall

CXX = g++
CXXFLAGS = -g -O0 -fno-strict-aliasing -Wno-write-strings -frtti -fexceptions -Wall -DDEBUG -std=c++11

LIBS = -lstdc++

ALL_VER := $(wildcard ./version ../version ../../version ../../../version)
VER := $(firstword $(ALL_VER))

ifneq ($(words $(ALL_VER)),1)
 $(error not found version file)
endif

PROJECT_ROOT := $(patsubst %/,%,$(dir $(VER)))

INCLUDE_DIR := $(PROJECT_ROOT)/include

SORTS_DIR := $(PROJECT_ROOT)/sorts
SORTS_SRC := $(wildcard $(SORTS_DIR)/*.cpp)
SORTS_OBJ := $(patsubst %.cpp,%.o,$(SORTS_SRC))

MISC_DIR := $(PROJECT_ROOT)/misc
MISC_SRC := $(wildcard $(MISC_DIR)/*.cpp)
MISC_OBJ := $(patsubst %.cpp,%.o,$(MISC_SRC))


DEST_TEST_SORTS := test_sorts
TEST_SORTS_OBJ := test_sorts.o

.PHONY : all

all: $(DEST_TEST_SORTS)


$(DEST_TEST_SORTS): $(TEST_SORTS_OBJ) $(SORTS_OBJ) $(MISC_OBJ)
	$(CXX) $(LIBS) -o $@ $^

####### Implicit rules ######

.SUFFIXES: .cpp .cc .cxx .c
.cpp.o:
	$(CXX) -c $(CXXFLAGS) -I$(INCLUDE_DIR) -o $@ $<

.cc.o:
	$(CXX) -c $(CXXFLAGS) -I$(INCLUDE_DIR) -o $@ $<

.cxx.o:
	$(CXX) -c $(CXXFLAGS) -I$(INCLUDE_DIR) -o $@ $<

.c.o:
	$(CC) -c $(CFLAGS) -I$(INCLUDE_DIR) -o $@ $<

clean:
	rm -rf $(SORTS_OBJ) $(MISC_OBJ) $(TEST_SORTS_OBJ) $(DEST_TEST_SORTS)

